package com.lucien.admin.controller.system;

import com.lucien.common.dto.RegistGroupDto;
import com.lucien.common.fornt.SysGroupFornt;
import com.lucien.common.fornt.SysUserFornt;
import com.lucien.common.result.GlobalResult;
import com.lucien.common.utils.StringUtils;
import com.lucien.system.domain.SysGroup;
import com.lucien.system.service.SysGroupService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author Lucien
 * @Date 2021/5/26 9:44
 * @Version 1.0
 * @Description: 群组控制器
 */
@RestController
@RequestMapping("/group")
@Api(tags = "群组接口")
public class SysGroupController {

    @Autowired
    private SysGroupService groupService;

    @PostMapping("/create/group")
    @ApiOperation(value = "创建群聊")
    public GlobalResult insertGroup(@RequestBody RegistGroupDto groupDto){
        int result = groupService.createGroup(groupDto);
        if (result == 1){
            return GlobalResult.success("创建成功", result);
        }else {
            return GlobalResult.error("创建失败~请稍后再尝试", result);
        }
    }

    @GetMapping("/getbygroupname")
    @ApiOperation(value = "获取群聊信息")
    public GlobalResult getGroupByName(String groupName, Integer creator){
        SysGroupFornt result = groupService.selectByName(groupName, creator);
        if (StringUtils.isNotNull(result)){
            return GlobalResult.success("查询成功", result);
        }else {
            return GlobalResult.error("未查询到该群聊", -1);
        }
    }

    @GetMapping("/bygroupid/{groupId}")
    @ApiOperation(value = "群聊ID获取群聊信息")
    public GlobalResult selectByGroupId(@PathVariable("groupId") Long groupId){
        SysGroupFornt result = groupService.selectByGroupId(groupId);
        if (StringUtils.isNotNull(result)){
            return GlobalResult.success("查询成功", result);
        }else {
            return GlobalResult.success("未查询到该群聊", -1);
        }
    }

    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除群聊")
    public GlobalResult delGroup(@PathVariable("id") Long id){
        int result = groupService.delGroup(id);
        if (result == 1){
            return GlobalResult.success("删除成功", result);
        }else {
            return GlobalResult.error("删除失败~请稍后再尝试", result);
        }
    }

    @GetMapping("/{userId}")
    @ApiOperation(value = "通过用户ID获取所在群聊列表")
    public GlobalResult groupList(@PathVariable("userId") Integer userId){
        List<SysGroup> result = groupService.groupList(userId);
        if(result.size() == 0){
            return GlobalResult.success("您还未加入仍和群聊", result);
        }
        return GlobalResult.success("查询成功", result);
    }

    @GetMapping("/member/{groupId}")
    @ApiOperation(value = "获取群聊成员")
    public GlobalResult getGroupMember(@PathVariable("groupId") Long groupId){
        List<SysUserFornt> result = groupService.getGroupMembers(groupId);
        return GlobalResult.success("查询成功", result);
    }

    @GetMapping("/groupid/{groupName}")
    @ApiOperation(value = "通过群名获取群ID")
    public GlobalResult getGroupIdByName(@PathVariable("groupName") String groupName){
        Integer result = groupService.getGroupIdByName(groupName);
        return GlobalResult.success("查询成功", result);
    }
}
